package com.stock.stockana.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.stock.stockana.R;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.requests.GetAllStocks;
import com.stock.stockana.ui.listener.OnStockItemClickedListener;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ---------------------------------------------------
 * Description:
 * Author: ljgsonx
 * Belong to: com.stock.stockana.Adapter
 * Time: 2016/1/20 17:14
 * ---------------------------------------------------
 */
public class StockListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    public Context mContext;
    public List<StockInfoBean> mDatas;
    public StockInfoBean[] fixedStock;
    public LayoutInflater mLayoutInflater;

    private final static int VIEWTYPE_FIXED = 0;
    private final static int VIEWTYPE_TITLE = 1;
    private final static int VIEWTYPE_CONTENT = 2;

    private boolean trendIsDesc;
    private boolean priceIsDesc;

    private final static int SORT_MODE_TREND = 0;
    private final static int SORT_MODE_PRICE = 1;

    private LinearLayout mNoItemTipLL;
    private LinearLayout mStockListTitleLL;

    private TextView mShPriceTv;
    private TextView mSzPriceTv;
    private TextView mCyPriceTv;
    private LinearLayout mShTrendLL;
    private LinearLayout mSzTrendLL;
    private LinearLayout mCyTrendLL;
    private ImageView mShArrowIv;
    private ImageView mSzArrowIv;
    private ImageView mCyArrowTv;
    private TextView mShTrendTv;
    private TextView mSzTrendTv;
    private TextView mCyTrendTv;

    public StockListAdapter(Context mContext) {
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDatas = GetAllStocks.stocks;
        fixedStock = GetAllStocks.fixedStock;
    }

    private OnStockItemClickedListener mStockItemClickedListener;

    public void setOnStockItemClickedListener(OnStockItemClickedListener listener) {
        this.mStockItemClickedListener = listener;
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == VIEWTYPE_FIXED) {
            View mView = mLayoutInflater.inflate(R.layout.stock_list_fixed, parent, false);
            return new StockHeaderHolder(mView);
        }else if(viewType == VIEWTYPE_TITLE){
            View mView = mLayoutInflater.inflate(R.layout.stock_list_header, parent, false);
            return new StockHeaderHolder(mView);
        }else{
            View mView = mLayoutInflater.inflate(R.layout.stocklist_item, parent, false);
            return new StocksHolder(mView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return VIEWTYPE_FIXED;
        }else if (position == 1) {
            return VIEWTYPE_TITLE;
        }else {
            return VIEWTYPE_CONTENT;
        }
    }

    /**
     * 绑定ViewHoler，给item中的控件设置数据
     */
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(position == 0) {
            configStockHeader(holder);
        }else if(position == 1){
            configStockTitle(holder);
        }else {
            configViewHolder((StocksHolder) holder, position - 2);
        }
    }

    private void configStockHeader(RecyclerView.ViewHolder holder) {

        mShPriceTv = (TextView) holder.itemView.findViewById(R.id.shPriceTv);
        mSzPriceTv = (TextView) holder.itemView.findViewById(R.id.szPriceTv);
        mCyPriceTv = (TextView) holder.itemView.findViewById(R.id.cyPriceTv);
        mShTrendLL = (LinearLayout) holder.itemView.findViewById(R.id.shTrendLL);
        mSzTrendLL = (LinearLayout) holder.itemView.findViewById(R.id.szTrendLL);
        mCyTrendLL = (LinearLayout) holder.itemView.findViewById(R.id.cyTrendLL);
        mShArrowIv = (ImageView) holder.itemView.findViewById(R.id.shArrowIv);
        mSzArrowIv = (ImageView) holder.itemView.findViewById(R.id.szArrowIv);
        mCyArrowTv = (ImageView) holder.itemView.findViewById(R.id.cyArrowIv);
        mShTrendTv = (TextView) holder.itemView.findViewById(R.id.shTrendTv);
        mSzTrendTv = (TextView) holder.itemView.findViewById(R.id.szTrendTv);
        mCyTrendTv = (TextView) holder.itemView.findViewById(R.id.cyTrendTv);


        DecimalFormat df = new DecimalFormat("0.000");
        if(fixedStock[0] == null || fixedStock[1] == null || fixedStock[2] == null) return;
        mShPriceTv.setText("￥" + df.format(fixedStock[0].getStPrice()));
        mSzPriceTv.setText("￥" + df.format(fixedStock[1].getStPrice()));
        mCyPriceTv.setText("￥" + df.format(fixedStock[2].getStPrice()));
        df = new DecimalFormat("0.00");
        if (fixedStock[0].getStTrend() > 0) {
            mShTrendLL.setBackgroundColor(mContext.getResources().getColor(R.color.c_stock_red));
            mShArrowIv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.up));
            mShTrendTv.setText("+" + df.format(fixedStock[0].getStTrend() * 100) + "%");
        }else{
            mShTrendLL.setBackgroundColor(mContext.getResources().getColor(R.color.c_stock_green));
            mShArrowIv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.down));
            mShTrendTv.setText(df.format(fixedStock[0].getStTrend() * 100) + "%");
        }
        if (fixedStock[1].getStTrend() > 0) {
            mSzTrendLL.setBackgroundColor(mContext.getResources().getColor(R.color.c_stock_red));
            mSzArrowIv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.up));
            mSzTrendTv.setText("+" + df.format(fixedStock[1].getStTrend() * 100) + "%");
        }else{
            mSzTrendLL.setBackgroundColor(mContext.getResources().getColor(R.color.c_stock_green));
            mSzArrowIv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.down));
            mSzTrendTv.setText(df.format(fixedStock[1].getStTrend() * 100) + "%");
        }
        if (fixedStock[2].getStTrend() > 0) {
            mCyTrendLL.setBackgroundColor(mContext.getResources().getColor(R.color.c_stock_red));
            mCyArrowTv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.up));
            mCyTrendTv.setText("+" + df.format(fixedStock[2].getStTrend() * 100) + "%");
        }else{
            mCyTrendLL.setBackgroundColor(mContext.getResources().getColor(R.color.c_stock_green));
            mCyArrowTv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.down));
            mCyTrendTv.setText(df.format(fixedStock[2].getStTrend() * 100) + "%");
        }
    }

    private void configStockTitle(final RecyclerView.ViewHolder holder) {
        mNoItemTipLL = (LinearLayout) holder.itemView.findViewById(R.id.noItemTipLL);
        mStockListTitleLL = (LinearLayout) holder.itemView.findViewById(R.id.stockListTitleLL);
        if (mDatas.size() > 0) {
            mNoItemTipLL.setVisibility(View.GONE);
            mStockListTitleLL.setVisibility(View.VISIBLE);
        }else{
            mNoItemTipLL.setVisibility(View.VISIBLE);
            mStockListTitleLL.setVisibility(View.GONE);
        }
        LinearLayout sortLL = (LinearLayout) holder.itemView.findViewById(R.id.sortStockLL);
        sortLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sortStocks(trendIsDesc, SORT_MODE_TREND);
                notifyDataSetChanged();
                trendIsDesc = !trendIsDesc;
            }
        });
        TextView spTv = (TextView) holder.itemView.findViewById(R.id.stockPriceTitleTv);
        spTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sortStocks(priceIsDesc, SORT_MODE_PRICE);
                notifyDataSetChanged();
                priceIsDesc = !priceIsDesc;
            }
        });
    }

    private void configViewHolder(final StocksHolder holder, final int position) {
        holder.stockNumTv.setText(mDatas.get(position).getStNum());
        holder.stockNameTv.setText(mDatas.get(position).getStName());
        if(mDatas.get(position).getStPrice() == 0){
            holder.stockPriceTv.setText("￥" + mDatas.get(position).getStPrice());
            holder.trendTv.setText((mDatas.get(position).getStTrend() * 100)+ "%");
            holder.stockPriceTv.setTextColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_light_gray2));
            holder.trendTv.setTextColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_white));
            holder.trendTv.setBackgroundColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_light_gray2));
        }else{
            DecimalFormat df = new DecimalFormat("0.000");
            String nowPrice = df.format(mDatas.get(position).getStPrice());
            holder.stockPriceTv.setText("￥" + nowPrice);
            df = new DecimalFormat("0.00");
            String pecentTrend = df.format(mDatas.get(position).getStTrend() * 100);
            if (mDatas.get(position).getStTrend() < 0){
                holder.trendTv.setText(pecentTrend+ "%");
                holder.stockPriceTv.setTextColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_stock_green));
                holder.trendTv.setTextColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_white));
                holder.trendTv.setBackgroundColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_stock_green));
            } else {
                holder.trendTv.setText("+"+pecentTrend+ "%");
                holder.stockPriceTv.setTextColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_stock_red));
                holder.trendTv.setTextColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_white));
                holder.trendTv.setBackgroundColor(mContext.getResources().getColor(com.stock.publiclib.R.color.c_stock_red));
            }
        }
        if (null != mStockItemClickedListener) {
            ((RippleView)holder.itemView.findViewById(R.id.stockItemRv)).setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                        mStockItemClickedListener.onStockItemCliked(mDatas.get(position));
                }
            });
        }
    }

    private void sortStocks(boolean isDesc,int sortMode) {
        StockInfoBean tempSb;
        boolean isSorted,needSwap;
        for (int i = 0; i < mDatas.size(); i++) {
            isSorted = true;
            for (int j = 1; j < mDatas.size()-i; j++) {
                if(sortMode == SORT_MODE_TREND)
                    needSwap = mDatas.get(j - 1).getStTrend() > mDatas.get(j).getStTrend();
                else
                    needSwap = mDatas.get(j - 1).getStPrice() > mDatas.get(j).getStPrice();
                needSwap = isDesc?!needSwap:needSwap;
                if (needSwap) {
                    tempSb = mDatas.get(j - 1);
                    mDatas.set(j - 1, mDatas.get(j));
                    mDatas.set(j, tempSb);
                    isSorted = false;
                }
            }
            if(isSorted) break;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size()+2;
    }

    class StocksHolder extends RecyclerView.ViewHolder{
        public TextView stockNumTv;
        public TextView stockNameTv;
        public TextView stockPriceTv;
        public TextView trendTv;

        public StocksHolder(View view) {
            super(view);
            this.stockNumTv = (TextView) view.findViewById(R.id.stockNumTv);
            this.stockNameTv = (TextView) view.findViewById(R.id.stockNameTv);
            this.stockPriceTv = (TextView) view.findViewById(R.id.stockPriceTv);
            this.trendTv = (TextView) view.findViewById(R.id.trendTv);
        }
    }

    class StockHeaderHolder extends RecyclerView.ViewHolder{

        public StockHeaderHolder(View itemView) {
            super(itemView);
        }
    }

}
